package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Classroom;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orgclassroommodule.service.ClassroomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.math.BigInteger;
import java.util.Date;

@Api(tags = "班级模块")
@RestController
@RequestMapping("/classroomManage")
public class ClassroomController {


    @Autowired
    private ClassroomService classroomService;

    @ApiOperation(value = "根据条件查看班级信息")
    @RequiresPermissions("/allClassroomInfo")
    @RequestMapping("/allClassroomInfo")
    public CommonResult allClassroomInfo(ServletRequest request, BigInteger branchId, String keyword, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取employeeId
        String employeeId = JWTUtil.getUsername(token);
        Classroom classroom = new Classroom();
        classroom.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        classroom.setBranchId(branchId);
        classroom.setEmployeeId(BigInteger.valueOf(Long.parseLong(employeeId)));
        classroom.setCreateTime(new Date());
        classroom.setUpdateTime(new Date());
        classroom.setClassroomName(keyword);
        return classroomService.allClassroomInfo(classroom, pageNum, pageSize);
    }

    @ApiOperation(value = "增加班级")
    @RequiresPermissions("/addClassroom")
    @RequestMapping("/addClassroom")
    public CommonResult addClassroom(ServletRequest request, Classroom classroom) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        classroom.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        classroom.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        classroom.setStudentCount(0);
        classroom.setCreateTime(new Date());
        classroom.setUpdateTime(new Date());
        classroom.setIsDel(0);
        return classroomService.addClassroom(classroom);
    }

    @ApiOperation(value = "修改班级")
    @RequiresPermissions("/editClassroom")
    @RequestMapping("/editClassroom")
    public CommonResult editClassroom(ServletRequest request, Classroom classroom) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);

        classroom.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        classroom.setUpdateTime(new Date());
        return classroomService.editClassroom(classroom);
    }

    @ApiOperation(value = "获取所有学生名")
    @RequestMapping("/getStudentOption")
    public CommonResult getStudentOption(ServletRequest request, BigInteger branchId) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        //暂时从token中获取branchid
        String branchIdtoken = JWTUtil.getBranchId(token);
//        return classroomService.getStudentOption(BigInteger.valueOf(Long.parseLong(organizationId)), branchId);
        return classroomService.getStudentOption(BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchIdtoken)));
    }

}
